package org.jeecg.modules.carRegister.util;

import cn.hutool.http.HttpRequest;
import cn.hutool.http.HttpResponse;
import cn.hutool.http.HttpUtil;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Component;

@Component
public class QueryEnv {

    public String getAngMei(String vin, String plateNo, String plateColor, String company) {
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("userid", "8f094429-ee2a-465c-85c5-8f7cc6206502");
        jsonObject.put("vin", vin);
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.MULTIPART_FORM_DATA);

        HttpResponse response = HttpRequest.post("https://www.51macc.com/api/Mattrio/VinInterface/query_vin_hgz").form(jsonObject.getInnerMap()).execute();
        String s = response.body();
        JSONObject result = JSONObject.parseObject(s);
        if (result.getInteger("recode") == 0) {
            String envType = result.getJSONObject("result").getJSONObject("Vechicle").getJSONObject("VechicleRoot").getString("PFBZ");
            if (envType.contains("国Ⅴ") || envType.contains("国五")) {
                addEnv(plateNo, plateColor, company, "国五");
                return "5";
            }
            if (envType.contains("国Ⅵ") || envType.contains("国六")) {
                addEnv(plateNo, plateColor, company, "国六");
                return "6";
            }
        }
        return null;
    }

    public String getLocal(String plateNo) {
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("PLATE", plateNo);
        String result = HttpUtil.post("https://enviroment.tw-tj.com:2443/ApiService/GetLocal", jsonObject.toString());
        JSONArray res = JSONObject.parseObject(result).getJSONArray("additional");
        if (res.isEmpty() || res.getJSONObject(0).getString("ENVIROMENT").equals("无结果")) {
            return null;
        }
        String enviroment = res.getJSONObject(0).getString("ENVIROMENT");
        switch (enviroment) {
            case "国一":
                return "1";
            case "国二":
                return "2";
            case "国三":
                return "3";
            case "国四":
                return "4";
            case "国五":
                return "5";
            case "国六":
                return "6";
            default:
                return null;
        }
    }

    public void addEnv(String plateNo, String plateColor, String company, String env) {
        JSONObject param = new JSONObject();
        param.put("entername", company);
        param.put("plate", plateNo);
        param.put("pcolor", plateColor);
        param.put("enviroment", env);
        param.put("cartype", "货车");
        String res = HttpUtil.post("https://enviroment.tw-tj.com:2443/ApiService/AddLocal", param.toString());
    }

    public String getZhongJiao(String plateNo, String plateColor, String company) {
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("plate", plateNo);
        jsonObject.put("pcolor", plateColor.equals("1") ? "2" : "1");
        jsonObject.put("entername", company);
        String result = HttpUtil.post("https://enviroment.tw-tj.com:2443/ApiService/GetZjxl", jsonObject);
        String env = JSONObject.parseObject(result).getString("message");
        if (env.contains("无结果")) {
            return null;
        }
        addEnv(plateNo, plateColor, company, env);
        if (env.contains("国五")) {
            return "5";
        }
        if (env.contains("国六")) {
            return "6";
        }
        return null;
    }
}
